<?php

class SeznamController extends Zend_Controller_Action
{

    public function preDispatch()
    {
        $this->view->render('seznam/_navigation.phtml');
    }

    public function init()
    {
        $this->_helper->layout->setLayout('layout');
        /* Initialize action controller here */
    }

    public function indexAction()
    {

        $this->_helper->redirector("index","index");
    }

    public function izdelkiAction()
    {
        // action body
    }

    public function podrobnostiAction()
    {


        $id=base64_decode($this->getParam('id'));

        $strankeSession = new Zend_Session_Namespace('Store_Stranke');

        $strankeSession->productId=$id;


// action body
    }

    public function prijavaAction()
    {
        $form    = new Application_Form_Stranka();
        $this->view->form = $form;


// action body
    }

    public function loginAction()
    {
        // action body

        $loginForm = new Application_Form_Stranka();

        if ($loginForm->isValid($_POST)) {
            $strankeSession = new Zend_Session_Namespace('Store_Stranke');
            $strankeSession->setExpirationSeconds(3600);
            $emso = $loginForm->getValue('emso');
            $geslo = $loginForm->getValue('geslo');

            $strankeMapper = new Application_Model_StrankaMapper();

            $strUser = $strankeMapper->authenticate($emso, $geslo);
            if ($strUser != null) {
                $strankeSession->strankaId = $strUser->getEmso();
                $strankeSession->ime = $strUser->getIme()." ".$strUser->getPriimek();


                $kosaricaMapper = new Application_Model_KosaricaMapper();


                $select = $kosaricaMapper->getDbTable()->getAdapter()
                    ->select()
                    ->from('kosarica')
                    ->where('stranka_emso = ?', $emso)
                    ->where('active = 1');


                $stmt = $kosaricaMapper->getDbTable()->getAdapter()->query($select);

                $kosarica = $stmt->fetchAll();
                $kosarica = $kosarica[0];

                if (isset($kosarica['sifra_kosarice']) && $kosarica['sifra_kosarice']) {
                    $strankeSession->kosaricaId = $kosarica['sifra_kosarice'];
                } else {

                }
                $this->_helper->redirector('index', 'seznam');
            } else {
                $strankeSession->error = "Napacni podatki!";
            }
            $this->_helper->redirector('prijava', 'seznam');

        
        
        }
    }

    public function logoutAction()
    {
        // action body
        $strankeSession = new Zend_Session_Namespace('Store_Stranke');
        if (!$strankeSession->strankaId) {
            $this->_helper->redirector('index', 'seznam');
        }
        $strankeSession->unsetAll();
        $this->_helper->redirector('index', 'seznam');

    }

    public function ustvariStrankoAction()
    {

        $form = new Application_Form_NovaStrankaReg();
        $this->view->form = $form;

        // action body
    }

    public function ustvariStrankoPostAction()
    {
        // action body    
        $strankaSession = new Zend_Session_Namespace('Store_Stranke');

        $editForm = new Application_Form_NovaStranka();

        // Polja v $editForm se napolnejo šele po klicu isValid
        if ($editForm->isValid($_POST)) {
            $emso = $editForm->getValue('emso');
            $ime = $editForm->getValue('ime');
            $priimek = $editForm->getValue('priimek');
            $email = $editForm->getValue('email');
            $naslov = $editForm->getValue('naslov');
            $telefon = $editForm->getValue('telefon');
            $geslo = $editForm->getValue('geslo');

            $strankaMapper = new Application_Model_StrankaMapper();

            $stranka = new Application_Model_Stranka();

            $stranka->setEmso($emso);
            $stranka->setIme($ime);
            $stranka->setPriimek($priimek);
            $stranka->setEmail($email);
            $stranka->setNaslov($naslov);
            $stranka->setTelefon($telefon);
            $stranka->setGeslo(password_hash($geslo, PASSWORD_DEFAULT));


            try {
                $strankaMapper->save($stranka);
                require_once('Zend/Mail.php');

                $mail = new Zend_Mail();
                $url = $this->view->serverUrl() . $this->view->baseUrl('seznam/aktiviraj-stranko/?hash='.$this->generirajHash($stranka).'&id='.base64_encode($stranka->getEmso()));
                $mail->setBodyHtml('Vas racun lahko aktivirate s klikom na naslednji url: <a href="'.$url.'">'.$url.'</a>');
                $mail->setFrom('veselielektroncek@gmail.com', 'Veseli Elektronček');
                $mail->addTo($stranka->getEmail(), $stranka->getIme()." ".$stranka->getPriimek());
                $mail->setSubject('Aktivacija računa');
                $mail->send();
                $strankaSession->success = "Shranjevanje stranke uspešno";
            } catch (Exception $e) {
                $strankaSession->error = $e->getMessage();
            }
        } else {
            $strankaSession->error = "Neveljaven post!";
        }

        $this->_helper->redirector('index', 'seznam');
    }

    public function generirajHash($stranka)
    {
        $token = md5(uniqid(mt_rand(), true));

        $strankaMapper = new Application_Model_StrankaMapper();
        $strankaMapper->update($stranka->getEmso(), 'token', $token);

        return $token;
    }

    public function editAction()
    {
        // action body

        $strankaSession = new Zend_Session_Namespace('Store_Stranke');

        if (!$strankaSession->strankaId) {
            $this->_helper->redirector('index', 'seznam');
            return;
        }
        $form    = new Application_Form_StrankaEdit();
        $this->view->form = $form;
    }

    public function editPostAction()
    {
        // action body
        $strankaSession = new Zend_Session_Namespace('Store_Stranke');
        $editForm = new Application_Form_StrankaEdit();
        $emso =  $strankaSession->strankaId;

        if ($strankaSession->strankaId) {
            // Polja v $editForm se napolnejo šele po klicu isValid
            if ($editForm->isValid($_POST)) {
                $geslo = $editForm->getValue('geslo');
                $strankaMapper = new Application_Model_StrankaMapper();
                $strankaUser = $strankaMapper->authenticate($emso, $geslo);
                if ($strankaUser != null) {
                    if ($editForm->getValue('spremeni_geslo')) {
                        if (strlen(trim($editForm)) > 0 && $editForm->getValue('geslo_novo') == $editForm->getValue('geslo_potrditev')) {
                            $strankaUser->setPriimek($editForm->getValue('priimek'));
                            $strankaUser->setIme($editForm->getValue('ime'));
                            $strankaUser->setEmail($editForm->getValue('email'));
                            $strankaUser->setNaslov($editForm->getValue('naslov'));
                            $strankaUser->setTelefon($editForm->getValue('telefon'));
                            $strankaUser->setGeslo(password_hash($editForm->getValue('geslo_novo'), PASSWORD_DEFAULT));
                            $strankaMapper->save($strankaUser);
                            $strankaSession->success = "Shranjevanje uspešno";

                        } else {
                            $strankaSession->error = "Napaka pri nastavljanju novega gesla. Prosim preveri podatke.";
                        }
                    } else {
                        $strankaUser->setPriimek($editForm->getValue('priimek'));
                        $strankaUser->setIme($editForm->getValue('ime'));
                        $strankaUser->setEmail($editForm->getValue('email'));
                        $strankaUser->setNaslov($editForm->getValue('naslov'));
                        $strankaUser->setTelefon($editForm->getValue('telefon'));

                        $strankaMapper->save($strankaUser);
                        $strankaSession->success = "Shranjevanje uspešno";
                    }
                } else {
                    $strankaSession->error = "Napačno geslo!";
                }
            }
        } else {
            $strankaSession->error = "Ta akcija je na voljo le stranki!";
        }
        $this->_helper->redirector('edit', 'seznam');
    }

    public function isciAction()
    {
        $isciForm = new Application_Form_SearchForm();
        if ($isciForm->isValid($_GET)) {
            $query = $isciForm->getValue('query');
            $mapper = new Application_Model_IzdelkiMapper();
            $where = $mapper->getDbTable()->getAdapter()->quoteInto('ime LIKE ?', "%".$query."%");

            $this->view->izdelki = $this->getIzdelki($where);

        
        }
    }

    public function getIzdelki($where = null)
    {
        $mapper = new Application_Model_IzdelkiMapper();
        return $mapper->getAllIzdelki($where);
    }

    public function oceniAction()
    {

        $oceniForm = new Application_Form_OcenaForm();
        $strankeSession = new Zend_Session_Namespace('Store_Stranke');
        $strankaEmso = $strankeSession->strankaId;
        if ($strankaEmso) {
            if ($oceniForm->isValid($_POST)) {
                $ocena = new Application_Model_Ocena();
                $ocena->setOcena($oceniForm->getValue('ocena'));
                $ocena->setOpis($oceniForm->getValue('opis'));
                $ocena->setIdProdukt($oceniForm->getValue('produktid'));
                $ocena->setStrankaEmso($strankaEmso);

                $mapper = new Application_Model_OcenaMapper();
                $mapper->save($ocena);

                $this->_redirect("/seznam/podrobnosti/?id=".base64_encode($ocena->getIdProdukt()));
            }
        } else {
            $strankeSession->error = 'Za ocenjevanje se je potrebno prijaviti';
        
        }
    }

    public function pretekliNakupiAction()
    {
        // action body
    }

    public function viewNarociloAction()
    {
        $narocilo = new Application_Model_Narocilo();
        $narocilo->loadById($this->getParam('id'));

        $this->view->narocilo = $narocilo;
    }

    public function aktivirajStrankoAction()
    {
        $hash = $this->getParam('hash');
        $emso = base64_decode($this->getParam('id'));
        $strankaMapper = new Application_Model_StrankaMapper();
        $stranka = new Application_Model_Stranka();
        $strankaMapper->find($emso, $stranka);
        $strankeSession = new Zend_Session_Namespace('Store_Stranke');

        if ($stranka->getToken() == $hash) {
            $strankaMapper->update($emso, 'aktiviran', 1);
            $strankeSession->success = 'Stranka uspešno aktivirana';
            $this->_helper->redirector('index', 'seznam');

        } else {
            $strankeSession->error = 'Napaka!';
            $this->_helper->redirector('index', 'seznam');

        }
    }


}



